<script setup lang="ts">

import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import Search from "@iconify-icons/ep/search";
import Refresh from "@iconify-icons/ep/refresh";
import { getOrderList } from "@/api/property";
import { reactive, ref } from "vue";
import { createOrder, payCheck, payOrder } from "@/api/user";
import router from "@/router";

const form = reactive({
  name: ""
});
const dataList = ref([]);
const onSearch = () => {

  console.log(router.currentRoute.value.query.id!=undefined)
  if (router.currentRoute.value.query.id!=undefined)
  {
    check(router.currentRoute.value.query.id)
  }else{
    getOrderList(form.name).then(res => {
      dataList.value = res.data;
    });
  }
};
const create = (data?: number) => {
  createOrder(data).then(res => {
    onSearch();
  });
};
const payForm = ref();
const pay = (data?: number) => {
  payOrder(data).then(res => {
    payForm.value.innerHTML = res.data;
    payForm.value.children[0].submit();
  })
};
const check = (data?: any) => {
  payCheck(data).then(res => {
    getOrderList(form.name).then(res => {
      dataList.value = res.data;
    });
  });
};
const loading = ref(false);
onSearch();
</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px]"
    >
      <el-form-item label="收费项目：" prop="name">
        <el-input
          v-model="form.name"
          placeholder="请输入收费项目"
          clearable
          class="!w-[180px]"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon(Search)"
          :loading="loading"
          @click="onSearch"
        >
          搜索
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          重置
        </el-button>
      </el-form-item>
      <br />
    </el-form>
    <el-table :data="dataList.records" table-layout="fixed">
      <el-table-column prop="id" label="ID" />
      <el-table-column label="收费金额">
        <template #default="scope">
          <el-tag type="warning">{{ scope.row.amount }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="created" label="收费时间" />
      <el-table-column prop="item" label="收费项目" />
      <el-table-column prop="outOrderId" label="支付订单号" />
      <el-table-column prop="userId" label="收费业主" />
      <el-table-column prop="status" label="收费状态">
        <template #default="scope">
          <el-tag v-if="scope.row.status===0" type="info">暂未创建订单</el-tag>
          <el-tag v-if="scope.row.status===1" type="danger">未支付</el-tag>
          <el-tag v-if="scope.row.status===2" type="success">已支付</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button
            type="danger"
            size="small"
            v-if="scope.row.status===0"
            @click="create(scope.row.id)"
          >创建订单
          </el-button>
          <el-button
            type="danger"
            size="small"
            v-if="scope.row.status===1"
            @click="pay(scope.row.id)"
          >立即支付
          </el-button>
          <el-button
            type="primary"
            size="small"
            v-if="scope.row.status===1"
            @click="check(scope.row.id)"
          >支付完成
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="width: 100%">
      <el-pagination
        style="float: right"
        background
        layout="prev, pager, next"
        :total="dataList.total"
        :page-count="dataList.current"
      />
    </div>
    <div ref="payForm"></div>
  </div>
</template>

<style scoped lang="scss">

</style>
